//
//  UPOderViewController.m
//  LoveFreshBee
//
//  Created by 王现涛 on 16/6/8.
//  Copyright © 2016年 王现涛. All rights reserved.
//
#define URLSTR @"http://m.beequick.cn/data/getCategoryProduct?asid=575d18dda79993326&_r=0.09407075636470152&tg=&tn=&reflogid=575d1fca7e9844954&zchtid=3248&bigids=2%2C0"

#import "UPOrderViewController.h"
#import <AFNetworking.h>
#import "UPOrderCategoryModel.h"
#import "UPOrderCategoryCell.h"
#define CATEGORYCELLID @"CATEGORY"
@interface UPOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *categoreTable;
//存放左边tableView的数据源
@property(nonatomic,strong)NSMutableArray *categoryDataSource;
@end

@implementation UPOrderViewController
-(NSMutableArray *)categoryDataSource{
    if (_categoryDataSource ==nil) {
        _categoryDataSource=[NSMutableArray array];
    }
    return _categoryDataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置上部栏颜色
    self.navigationController.navigationBar.barTintColor=UPColor(255, 218, 0);
//设置categoryTable的背景颜色
    self.categoreTable.backgroundColor=UPColor(248, 248, 248);
    [self setNavBar];
    [self sendRequest];//调用
    [self registerXib];
}
-(void)setNavBar{
    self.navigationController.navigationBar.barTintColor=UPColor(255, 218, 0);
    //设置左边的按钮
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [btn addTarget:self  action:@selector(btnLeftClick) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundImage:[UIImage imageNamed:@"icon_black_scancode" ]forState:UIControlStateNormal];
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem=leftItem;
    
    UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(320, 0, 30, 30)];
    [btn1 addTarget:self action:@selector(btnRightClick) forControlEvents:UIControlEventTouchUpInside];
   [ btn1 setBackgroundImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithCustomView:btn1];
    self.navigationItem.rightBarButtonItem=rightItem;
}
/**
 *  注册tableView的cell为xib
 */
-(void)registerXib{
    //如果一个方法的形参是bundle的话 这设置为nil的话 就代表[NSBundle mainbundle]
    UINib *categoryNib=[UINib nibWithNibName:@"UPOrderCategoryCell" bundle:nil];
    [self.categoreTable registerNib:categoryNib forCellReuseIdentifier:CATEGORYCELLID];
}

//点击左边按钮调用此方法
-(void)btnLeftClick{

}
//点击右边按钮调用此方法
-(void)btnRightClick{

}
//向服务器发送请求 ，就可以得到从服务器返回的数据
-(void)sendRequest{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager GET:URLSTR parameters:nil success:^(NSURLSessionDataTask *task, NSDictionary *responseDict) {
        NSDictionary *dataDict=responseDict[@"data"];
        if (dataDict==nil) {
            NSString *path=[[NSBundle mainBundle]pathForResource:@"loveFreshBee.plist" ofType:nil];
            responseDict=[NSDictionary dictionaryWithContentsOfFile:path];
            dataDict=responseDict[@"data"];
        }
        NSArray *categories=dataDict[@"categories"];
        for (NSDictionary *categoryDict in categories) {
            UPOrderCategoryModel *model=[UPOrderCategoryModel categoryWithDict:categoryDict];
            [self.categoryDataSource addObject:model];
        }
        //刷新左边的tableView
        [self.categoreTable reloadData];
        NSIndexPath *path=[NSIndexPath indexPathForRow:0 inSection:0];//第一组的第一行
        [self.categoreTable selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionTop];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
#pragma mark - tableView的数据源代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==self.categoreTable) {
        return self.categoryDataSource.count;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==self.categoreTable) {
        //从缓存池中获取可用的cell 如果没有 就以xib所对应的cell为模板进行创建  因此不再需要判断为空了
    
        UPOrderCategoryCell *cell=[tableView dequeueReusableCellWithIdentifier:CATEGORYCELLID];
        UPOrderCategoryModel *model=self.categoryDataSource[indexPath.row];
        cell.model=model;
        return cell;
    }else{
        return [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"a"];
    }
    
}



@end

